package com.zbkj.crmeb.task.monitor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.utils.CrmebUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zbkj.crmeb.category.model.Category;
import com.zbkj.crmeb.system.dao.SystemAdminDao;
import com.zbkj.crmeb.system.model.SystemAdmin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @PROJECT_NAME: crmeb-server
 * @DESCRIPTION:
 * @USER: wsl
 * @DATE: 2023/11/20 20:25
 */
@Component
@Configuration
@Slf4j
public class ResetAllUserPassword {

    @Resource
    private SystemAdminDao systemAdminDao;
    @XxlJob("resetPassword")
    public void reset(){
        String password = "notguess";
        LambdaQueryWrapper<SystemAdmin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemAdmin::getIsDel,"0");
        List<SystemAdmin> systemAdmins = systemAdminDao.selectList(lambdaQueryWrapper);
        log.info("重置密码size: {},{}",systemAdmins.size(),password);

        for (SystemAdmin systemAdmin : systemAdmins) {
            String newPassword = CrmebUtil.encryptPassword(password,systemAdmin.getAccount());
            systemAdmin.setPwd(newPassword);
            systemAdmin.setUpdateTime(new Date());
            try {
                systemAdminDao.updateById(systemAdmin);
            }catch (Exception e){
                log.error("重置密码异常: ",e);
            }
        }

    }
}
